package com.sanpu.kxe.business.card.view;

import android.content.Context;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.card.bean.DefBankOcrOutput;
import com.sanpu.kxe.business.card.contract.CardConfig;
import com.sanpu.kxe.business.card.contract.UpdateBankSecondContract;
import com.sanpu.kxe.business.card.presenter.UpdateBankSecondPresenter;
import com.sanpu.kxe.common.utils.CommonUtils;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.module.widget.EditTextWithDel;

import butterknife.Bind;

/**
 * Description：修改默认结算卡第二步
 * Created by 薛勇军 on 2019/5/15.
 */

public class UpdateBankSecondActivity extends BaseTitleActivity implements UpdateBankSecondContract.View {

    @Bind(R.id.etBankNumber)
    EditTextWithDel etBankNumber;
    @Bind(R.id.etPhoneNumber)
    EditTextWithDel etPhoneNumber;
    @Bind(R.id.commit)
    RoundTextView vCommit;

    private UpdateBankSecondContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_update_bank_second;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("修改结算卡");

        DefBankOcrOutput ocrOutput =
                (DefBankOcrOutput) getIntent().getSerializableExtra(CardConfig.INTENT_DEFBANKOCR_OUTPUT);
        if (ocrOutput != null && !TextUtils.isEmpty(ocrOutput.getCardNo())) {
            etBankNumber.setText(ocrOutput.getCardNo());
        }
        mPresenter = new UpdateBankSecondPresenter(this, ocrOutput);
    }

    @Override
    protected void setListener() {
        super.setListener();
        vCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isRespondEvent()) return;
                String bankNo = etBankNumber.getText().toString().replace(" ", "");
                String redTel = etPhoneNumber.getText().toString();
                mPresenter.modifyDefUrmBank(bankNo, redTel);
            }
        });
        etBankNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String current = s.toString().replace(" ", "");
                String format = CommonUtils.bankNumFormatter(current);
                etBankNumber.removeTextChangedListener(this);
                etBankNumber.setText(format);
                etBankNumber.addTextChangedListener(this);
            }

            @Override
            public void afterTextChanged(Editable s) {
                CharSequence text = etBankNumber.getText();
                if (text != null) {
                    Spannable spanText = (Spannable) text;
                    Selection.setSelection(spanText, text.length());
                }
            }
        });
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(UpdateBankSecondContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
